package cc.miwuya.property.service.impl;

import cc.miwuya.common.util.Result;
import cc.miwuya.property.client.UserClient;
import cc.miwuya.property.domain.Meter;
import cc.miwuya.property.service.MeterService;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.miwuya.property.domain.House;
import cc.miwuya.property.service.HouseService;
import cc.miwuya.property.mapper.HouseMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 迷雾森林
* @description 针对表【house(楼宇表)】的数据库操作Service实现
* @createDate 2024-02-22 19:52:54
*/
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House>
    implements HouseService{
    @Resource
    private UserClient userClient;

    @Resource
    private MeterService meterService;

    @Override
    public List<Long> getHouseNumberList(Long residentialId) {
        List<Long> list = listObjs(new QueryWrapper<House>().eq("residential_id", residentialId).groupBy("house_number").select("house_number"));
        return list;
    }

    @Override
    public List<House> getUnitNumber(Long residentialId, Long houseNumber) {
        //new QueryWrapper<House>().eq("residential_id",residentialId).select("DISTINCT house_number"))
        List<House> list = list(new QueryWrapper<House>().eq("residential_id", residentialId).eq("house_number", houseNumber).select("DISTINCT unit_number"));

        return list;
    }

    @Override
    public Object getRoomNumberList(Page page,Long residentialId, Long houseNumber, Long unitNumber) {
        Page<House> housePage = null;
        housePage = page(page,new QueryWrapper<House>().eq("residential_id", residentialId).eq("house_number", houseNumber).eq("unit_number",unitNumber));
//        List<Long> list = listObjs(new QueryWrapper<House>().eq("residential_id", residentialId).eq("house_number", houseNumber).eq("unit_number",unitNumber).select("room_number"));
        return housePage;
    }

    @Override
    public Object getHouseInfoByHouseId(Long houseId) {
        House house = getById(houseId);
        Result info = userClient.ownerInfo(house.getOwnerId());
        List<Meter> meters = meterService.list(new QueryWrapper<Meter>().eq("home_id", house.getId()));
        return MapUtil.builder()
                .put("house",house)
                .put("ownerInfo",info.getData())
                .put("meter",meters)
                .build();
    }

    @Override
    public Object getHouseAllByResidential(Page page, Long residentialId) {
        Page<House> housePage = null;
        housePage = page(page,new QueryWrapper<House>().eq("residential_id",residentialId));
        return housePage;
    }

    @Override
    public Object getHouseAllByHouse(Page page, Long residentialId, Long houseId) {
        Page<House> housePage = null;
        housePage = page(page,new QueryWrapper<House>().eq("residential_id",residentialId).eq("house_number",houseId));
        return housePage;
    }

    @Override
    public Object getOwnerHouseList(Page page, Long ownerId, Long residentialId) {
        Page<House> housePage = null;
        if (residentialId==null){
            housePage = page(page,new QueryWrapper<House>().eq("owner_id",ownerId));
        }else {
            housePage = page(page,new QueryWrapper<House>().eq("owner_id",ownerId).eq("residential_id",residentialId));
        }

        return housePage;
    }
}




